package org.developerworks.lift.model

import _root_.net.liftweb.mapper._
import _root_.net.liftweb.util._

object Item extends Item with KeyedMetaMapper[Long, Item] with CRUDify[Long, Item]{
     override def dbTableName = "items"
     override def fieldOrder = List(name, description, reserve, expiration) 
}
class Item extends KeyedMapper[Long, Item] {   
     
  def getSingleton = Item  
     
  def primaryKeyField = id

     object id extends MappedLongIndex(this)
     object reserve extends MappedInt(this)
     object name extends MappedString(this, 100)
     object description extends MappedText(this)
     object expiration extends MappedDateTime(this) 
  
     lazy val detailUrl = "/details.html?itemId=" + id.is  
     def bids = Bid.findAll(By(Bid.item, id))
  
     def tr = {
      
      <tr>
        <td><a href={detailUrl}>{name}</a></td>
        <td>{highBid.amount}</td>
        <td>{expiration}</td>
      </tr>
  }
  def highBid:Bid = {
    val allBids = bids
    if (allBids.size > 0){
      return allBids.sort(_.amount.is > _.amount.is)(0)
    }
    Bid.create
  }

}